#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <linux/in.h>

int main(){
	int ret = 0;
	int sfd = 0;
	int cfd = 0;
	struct sockaddr_in s_add, c_add;
	int sin_size;
	char buffer[1024] = {0};
	unsigned short port = 8080;

	//1. 建立socket
	cfd = socket(AF_INET, SOCK_STREAM, 0);
	if(-1 == cfd){
		printf("build socket error:%m\n");
		return -1;
	}
	printf("build socket ok!\n");

	//2. 组建服务器地址
	memset(&s_add, 0, sizeof(struct sockaddr_in));
	s_add.sin_family = AF_INET;
	s_add.sin_addr.s_addr = inet_addr("127.0.0.1");
	s_add.sin_port = htons(port);

	//3. 连接服务器
	ret = connect(cfd, (struct sockaddr*)(&s_add), sizeof(struct sockaddr));
	if(-1 == ret){
		printf("connect error:%m\n");
		return -1;
	}
	printf("connect ok!\n");

	//4. 接受数据
	ret = read(cfd, buffer, 1024);
	if(-1 == ret){
		printf("read date error:%m\n");
		return -1;
	}else{
		buffer[ret] = '\n';
		printf("recv:%s\n", buffer);
	}

	close(cfd);

	


}
